/*
 * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zdj.data.daoGenerator;

import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;
import de.greenrobot.daogenerator.ToMany;

/**
 * Generates entities and DAOs for the example project DaoExample.
 *
 * Run it as a Java application (not Android).
 *
 * @author Markus
 */
public class CustomDaoGenerator {

    public static void main(String[] args) throws Exception {
        // 参数2 是run config的work dir的路径 加上下面的相对路径下的包名
        Schema schema = new Schema(1001, "com.zdj.data.entity");

        addNote(schema);
        addCustomerOrder(schema);
        addUser(schema);
//        addLogin(schema);
        addHomePageData(schema);
        addLoginInfo(schema);

        // 参数2 是 相对于run config 中的work dir 的相对路径
        new DaoGenerator().generateAll(schema, "./");
    }

    private static void addLoginInfo(Schema schema){
        Entity loginInfo = schema.addEntity("LoginInfo");
        // 添加一个id主键
        loginInfo.addIdProperty();

        loginInfo.addStringProperty("admin_id");
        loginInfo.addStringProperty("already_bind");
        loginInfo.addStringProperty("children_num");
        loginInfo.addStringProperty("client");
        loginInfo.addLongProperty("create_time");
        loginInfo.addStringProperty("grade");
        loginInfo.addStringProperty("head_img");
        loginInfo.addIntProperty("uid");
        loginInfo.addStringProperty("mobile");
        loginInfo.addStringProperty("name");
        loginInfo.addStringProperty("password");
        loginInfo.addStringProperty("room_id");
        loginInfo.addStringProperty("school_id");
        loginInfo.addStringProperty("semester");
        loginInfo.addStringProperty("sex");
        loginInfo.addStringProperty("state");
        loginInfo.addStringProperty("student_id");
        loginInfo.addStringProperty("student_no");
        loginInfo.addStringProperty("user_type");
        loginInfo.addStringProperty("vip_id");
    }


    private static void addHomePageData(Schema schema){
        Entity homePageData = schema.addEntity("HomePageData");
        // 添加一个id主键
        homePageData.addIdProperty();
        homePageData.addIntProperty("homework_num");
        homePageData.addIntProperty("done_homework_num");
        homePageData.addIntProperty("err_question_num");
        homePageData.addIntProperty("favorite_question_num");
        homePageData.addIntProperty("practice_num");
        homePageData.addIntProperty("exam_num");
    }

    private static void addLogin(Schema schema){
        Entity login = schema.addEntity("Login");
        login.addIdProperty();
        login.addStringProperty("username");
        login.addStringProperty("password");
    }


    private static void addUser(Schema schema){
        Entity note = schema.addEntity("User");
        note.addIntProperty("id");
        note.addStringProperty("cover_url");
        note.addStringProperty("full_name");
        note.addStringProperty("email");
        note.addStringProperty("description");
        note.addIntProperty("followers");
    }


    private static void addNote(Schema schema) {
        Entity note = schema.addEntity("Note");
        note.addIdProperty();
        note.addStringProperty("text").notNull();
        note.addStringProperty("comment");
        note.addDateProperty("date");
    }

    private static void addCustomerOrder(Schema schema) {
        Entity customer = schema.addEntity("Customer");
        customer.addIdProperty();
        customer.addStringProperty("name").notNull();

        Entity order = schema.addEntity("Order");
        // 避免与sql的关键字冲突
        order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
        order.addIdProperty();
        Property orderDate = order.addDateProperty("date").getProperty();
        Property customerId = order.addLongProperty("customerId").notNull().getProperty();
        order.addToOne(customer, customerId);

        ToMany customerToOrders = customer.addToMany(order, customerId);
        customerToOrders.setName("orders");
        customerToOrders.orderAsc(orderDate);
    }

}
